package de.se.ludoapp.activity.task;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.widget.Button;
import android.widget.Toast;
import de.se.ludoapp.LudoApplication;
import de.se.ludoapp.R;
import de.se.ludoapp.activity.LoginRememberActivity;
import de.se.ludoapp.activity.RegistrationActivity;
import de.se.ludoapp.activity.Select_menuActivity;
import de.se.ludoapp.gaming.User;
import de.se.ludoapp.response.UserLoginResponse;

/**
 * Task for login and logout of the user in communication with the server.
 * 
 * @author L.Rother
 * 
 */
public class RegisterTask extends AsyncTask<String, Integer, String> {

	private LudoApplication ludoApp;
	private SharedPreferences prefs;
	private RegistrationActivity context;
	private String userName, pwd, sndPwd;

	public RegisterTask(LudoApplication ludoApp, SharedPreferences prefs,
			RegistrationActivity loginRememberActivity, String userName,
			String pwd, String sndPwd) {
		super();
		this.ludoApp = ludoApp;
		this.prefs = prefs;
		this.context = loginRememberActivity;
		this.userName = userName;
		this.pwd = pwd;
		this.sndPwd = sndPwd;
	}

	/**
	 * hadels login and logout with server. <br>
	 * This method is executed in a concurrent thread (asynchronous). Therefore
	 * you mustn't accessed on view elements!
	 */
	@Override
	protected String doInBackground(String... params) {
		String output = null;
		if (ludoApp.getlogedinUser() == null) {
			/* Execute Login */
			UserLoginResponse userLoginResponse = ludoApp.getLudoStub()
					.register(userName, pwd);
			if (userLoginResponse.hasResponseErrors()) {
				Toast.makeText(context, userLoginResponse.getMsg(),
						Toast.LENGTH_LONG).show();
			} else {
				User logedinUser = new User(userLoginResponse.getUserName(),
						pwd, userLoginResponse.getSessionId());
				ludoApp.setLogedinUser(logedinUser);
				output = "Sie sind eingeloggt als " + logedinUser.getUsername();
			}

		} else {
			/* Execute Logout */
			ludoApp.getLudoStub().logout(ludoApp.getlogedinUser());
			ludoApp.setLogedinUser(null);
			output = "Bitte erneut einloggen";
		}
		return output;
	}

	/**
	 * show login/logout results. <br>
	 * This method is invoked when the LoginTask is executed. You are only
	 * allowed here to access on view Elements (TextView and Buttons)!
	 */
	@Override
	protected void onPostExecute(String result) {

		Button buttonReg = (Button) context.findViewById(R.id.btnRegister);
		buttonReg.setEnabled(true);
		if (ludoApp.getlogedinUser() == null) {
			buttonReg.setEnabled(false);
			buttonReg.setText(R.string.buttonRegister);
			Intent loginMenu = new Intent(context, LoginRememberActivity.class);
			context.startActivity(loginMenu);
		} else {
			buttonReg.setText(R.string.buttonLogout);
			Intent intentSelectMenu = new Intent(context,
					Select_menuActivity.class);
			context.startActivity(intentSelectMenu);

		}
	}
}
